package org.jeecg.modules.practice.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.common.bean.RabbitMqConstant;
import org.jeecg.modules.common.rabbitmq.RabbitmqUtil;
import org.jeecg.modules.common.validation.CreateNotNull;
import org.jeecg.modules.common.feign.ErpFeignClient;
import org.jeecg.modules.practice.entity.Practice;
import org.jeecg.modules.practice.service.IPracticeService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Arrays;
import java.util.Set;

 /**
 * @Description: 从业人员表
 * @Author: jeecg-boot
 * @Date:   2023-11-26
 * @Version: V1.0
 */
@Api(tags="从业人员表")
@RestController
@RequestMapping("/sys/practice")
@Slf4j
public class PracticeController extends JeecgController<Practice, IPracticeService> {

	@Resource
	private RabbitmqUtil rabbitmqUtil;

	 @Resource
	 private RabbitTemplate rabbitTemplate;

	@Resource
	private Validator validator;

	@Autowired
	private IPracticeService practiceService;

	@Resource
	private ErpFeignClient erpFeignClient;

	/**
	 * 分页列表查询
	 *
	 * @param practice
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "从业人员表-分页列表查询")
	@ApiOperation(value="从业人员表-分页列表查询", notes="从业人员表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<Practice>> queryPageList(Practice practice,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Practice> queryWrapper = QueryGenerator.initQueryWrapper(practice, req.getParameterMap());
		Page<Practice> page = new Page<Practice>(pageNo, pageSize);
		IPage<Practice> pageList = practiceService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param practice
	 * @return
	 */
	@AutoLog(value = "从业人员表-添加")
	@ApiOperation(value="从业人员表-添加", notes="从业人员表-添加")
	@RequiresPermissions("practice:practice:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Practice practice) {

		Set<ConstraintViolation<Practice>> validate = validator.validate(practice, CreateNotNull.class);
		if (!validate.isEmpty()) {
			// 处理校验失败的情况
			validate.forEach(item -> {
				//输出验证错误信息
				System.out.println(item.getMessage());
			});

			return Result.error(validate.toString());
		}

		// 新增前先去erp查询是否已有用户
		Result<Practice> pracByIdCard = erpFeignClient.getPracByIdCard(practice.getIdcard());
		Practice result = pracByIdCard.getResult();
		if (result != null) {
			return Result.error("该用户已注册！证件号["+practice.getIdcard()+"]已被使用");
		}

		practiceService.save(practice);
		// 1. 客户端初始化rabbitMq
		rabbitmqUtil.archivesBinding(RabbitMqConstant.CONVENIENT_EXCHANGE, RabbitMqConstant.CONVENIENT_QUEUE, RabbitMqConstant.CONVENIENT_KEY);
		// 2. 客户端发送消息
		rabbitTemplate.convertAndSend(RabbitMqConstant.CONVENIENT_EXCHANGE, RabbitMqConstant.CONVENIENT_KEY, JSONObject.toJSONString(practice));
		log.info("system模块请求： {}", JSONObject.toJSONString(practice));
		return Result.OK("添加成功！");
	}

	@GetMapping(value = "/demo")
	public void demo() {
		 // 1. 客户端初始化rabbitMq
		 rabbitmqUtil.archivesBinding(RabbitMqConstant.CONVENIENT_EXCHANGE, RabbitMqConstant.CONVENIENT_QUEUE, RabbitMqConstant.CONVENIENT_KEY);
		// 2. 客户端发送消息
		String str = "{\"address\":\"十里河\",\"birtday\":1701360000000,\"createBy\":\"admin\",\"createTime\":1702099261098,\"driving\":\"B1,C1\",\"drivliceEndDate\":1706630400000,\"drivliceStartDate\":1701360000000,\"education\":\"100\",\"email\":\"13240031252\",\"id\":\"1733356049706348545\",\"idcard\":\"372022202210101212\",\"mobile\":\"18512655525\",\"photoPath\":\"temp/图片1_1702099243267.png\",\"pracname\":\"白敬亭\",\"sex\":\"1\",\"sysOrgCode\":\"A01\"}";
		Practice practice = JSONObject.parseObject(str, Practice.class);
		rabbitTemplate.convertAndSend(RabbitMqConstant.CONVENIENT_EXCHANGE, RabbitMqConstant.CONVENIENT_KEY, JSONObject.toJSONString(practice));
		log.info("system模块请求： {}", JSONObject.toJSONString(practice));
	 }

	 @GetMapping(value = "/demo2")
	 public void demo2() {
		 String str = "{\"address\":\"十里河\",\"birtday\":1701360000000,\"createBy\":\"admin\",\"createTime\":1702099261098,\"driving\":\"B1,C1\",\"drivliceEndDate\":1706630400000,\"drivliceStartDate\":1701360000000,\"education\":\"100\",\"email\":\"13240031252\",\"id\":\"1733356049706348545\",\"idcard\":\"372022202210101212\",\"mobile\":\"18512655525\",\"photoPath\":\"temp/图片1_1702099243267.png\",\"pracname\":\"白敬亭\",\"sex\":\"1\",\"sysOrgCode\":\"A01\"}";
		 Practice practice = JSONObject.parseObject(str, Practice.class);
		 // 初始化rabbitMq
		 rabbitmqUtil.archivesBinding(RabbitMqConstant.TRANS_EXCHANGE, RabbitMqConstant.TRANS_QUEUE, RabbitMqConstant.TRANS_KEY);
		 rabbitTemplate.convertAndSend(RabbitMqConstant.TRANS_EXCHANGE, RabbitMqConstant.TRANS_KEY, JSONObject.toJSONString(practice));
		 log.info("system模块请求： {}", JSONObject.toJSONString(practice));
	 }

	 @GetMapping(value = "/demo3")
	 public void demo3() {
		 String str = "{\"address\":\"十里河\",\"birtday\":1701360000000,\"createBy\":\"admin\",\"createTime\":1702099261098,\"driving\":\"B1,C1\",\"drivliceEndDate\":1706630400000,\"drivliceStartDate\":1701360000000,\"education\":\"100\",\"email\":\"13240031252\",\"id\":\"1733356049706348545\",\"idcard\":\"372022202210101212\",\"mobile\":\"18512655525\",\"photoPath\":\"temp/图片1_1702099243267.png\",\"pracname\":\"白敬亭\",\"sex\":\"1\",\"sysOrgCode\":\"A01\"}";
		 Practice practice = JSONObject.parseObject(str, Practice.class);
		 // 初始化rabbitMq
		 rabbitmqUtil.archivesBinding(RabbitMqConstant.TRANS_EXCHANGE, RabbitMqConstant.TRANS_QUEUE_TB, RabbitMqConstant.TRANS_KEY);
		 rabbitTemplate.receive(RabbitMqConstant.TRANS_QUEUE_TB);
		 rabbitTemplate.convertAndSend(RabbitMqConstant.TRANS_EXCHANGE, RabbitMqConstant.TRANS_KEY, JSONObject.toJSONString(practice));

		 log.info("system模块请求： {}", JSONObject.toJSONString(practice));
	 }

	/**
	 *  编辑
	 *
	 * @param practice
	 * @return
	 */
	@AutoLog(value = "从业人员表-编辑")
	@ApiOperation(value="从业人员表-编辑", notes="从业人员表-编辑")
	@RequiresPermissions("practice:practice:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Practice practice) {
		practiceService.updateById(practice);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "从业人员表-通过id删除")
	@ApiOperation(value="从业人员表-通过id删除", notes="从业人员表-通过id删除")
	@RequiresPermissions("practice:practice:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		practiceService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "从业人员表-批量删除")
	@ApiOperation(value="从业人员表-批量删除", notes="从业人员表-批量删除")
	@RequiresPermissions("practice:practice:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.practiceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "从业人员表-通过id查询")
	@ApiOperation(value="从业人员表-通过id查询", notes="从业人员表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Practice> queryById(@RequestParam(name="id",required=true) String id) {
		Practice practice = practiceService.getById(id);
		if(practice==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(practice);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param practice
    */
    @RequiresPermissions("practice:practice:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Practice practice) {
        return super.exportXls(request, practice, Practice.class, "从业人员表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("practice:practice:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Practice.class);
    }

}
